#include "Hub.h"
#include "game.h"

extern Game* pCurrentGame;		// Hacky. See gamecode.cpp

HubControl::HubControl()
{
	isActive = true;
	theCurrentView = LIST;
}

bool HubControl::GetActive()const
{
	return isActive;
}

void HubControl::setActive(bool value)
{
	isActive = value;
}

void HubControl::init()
{
	gTheMouse.Initialise(SCREENWIDTH,SCREENHEIGHT);

	g_theGalaxy.init(planetNames);
	g_theLists.init(planetNames);

	char tempText1[] = {"View Change"};
	char tempText2[] = {"Options"};
	char tempText3[] = {"Exit Game"};


	theViewButton.Initialise((SCREENHEIGHT-50),((SCREENWIDTH/2)-210),(SCREENHEIGHT-25),((SCREENWIDTH/2)-90),
		tempText1);
	theOptionsButton.Initialise((SCREENHEIGHT-50),((SCREENWIDTH/2)-50),(SCREENHEIGHT-25),((SCREENWIDTH/2)+50),
		tempText2);
	theExitButton.Initialise((SCREENHEIGHT-50),((SCREENWIDTH/2)+100),(SCREENHEIGHT-25),((SCREENWIDTH/2)+200),
		tempText3);

	thePopUps.init();

	strcpy_s(planets[0].planetName, "AppleDrop");
	planets[0].planetNo = 1;
	planets[0].theGameCreator = GetAppledropGame;

	strcpy_s(planets[1].planetName, "Space Paranoids");
	planets[1].planetNo = 2;
	planets[1].theGameCreator = GetSpaceInvadersGame;

	strcpy_s(planets[2].planetName, "PongGame");
	planets[2].planetNo = 3;
	planets[2].theGameCreator = GetPongGame;

	//strcpy_s(planets[3].planetName, "Blah1");
	//planets[3].planetNo = 4;
	//planets[3].theGameCreator = GetPongGame;

	//strcpy_s(planets[4].planetName, "Blah2");
	//planets[4].planetNo = 5;
	//planets[4].theGameCreator = GetPongGame;

	//strcpy_s(planets[5].planetName, "Blah3");
	//planets[5].planetNo = 6;
	//planets[5].theGameCreator = GetPongGame;

	for (int i = 0; i < currentImpPlanets; i++)
	{
		planets[i].positionNo = planets[i].planetNo%3;
		if (i%3 == 0)
		{
			planets[i].backdrop.placing((SCREENHEIGHT/2),(100),
				((SCREENHEIGHT/2)+200),(300));

			planets[i].startButton.Initialise((SCREENHEIGHT/2+150),(125),
				((SCREENHEIGHT/2)+175),(175),"Start");
		}
		else if (i%3 == 1)
		{
			planets[i].backdrop.placing((SCREENHEIGHT/2),(400),
				((SCREENHEIGHT/2) + 200),(600));

			planets[i].startButton.Initialise((SCREENHEIGHT/2+150),(425),
				((SCREENHEIGHT/2) + 175),(475),"Start");
		}
		else if (i%3 == 2)
		{
			planets[i].backdrop.placing((SCREENHEIGHT/2),(700),
				((SCREENHEIGHT/2) + 200),(900));

			planets[i].startButton.Initialise(((SCREENHEIGHT/2)+150),(725),
				((SCREENHEIGHT/2) + 175),(775),"Start");
		}

	}

	currentScore = 0;
	currentMoney = 3000;
	currentFuel = 10;
}

void GalaxyMap::init(char thePlanets[15][activeProjects])
{
	for (int i = 0; i < activeProjects; i++)
	{
		for (int k = 0; k< 15; k++)
		{
			planetNames[k][i] = thePlanets[k][i];
		}
	}

	theMainMap = MyDrawEngine::GetInstance()->LoadPicture("galaxy.jpg");
}

int ListView::GetCurrentView()
{
	return currentView;
}

void ListView::init(char thePlanets[15][activeProjects])
{
	for (int i = 0; i < activeProjects; i++)
	{
		for (int k = 0; k< 15; k++)
		{
			planetNames[k][i] = thePlanets[k][i];
		}
	}

	theBackdrop = MyDrawEngine::GetInstance()->LoadPicture("list.jpg");

	currentView = 1;

	left.Initialise((SCREENHEIGHT-100),(SCREENWIDTH-50),(SCREENHEIGHT-75),(SCREENWIDTH-100),"Next");
	right.Initialise((SCREENHEIGHT-100),(SCREENWIDTH-120),(SCREENHEIGHT-75),(SCREENWIDTH-170),"Back");
}

void HubControl::update(Game* &pCurrentGame)
{
	MyInputs* pInputs = MyInputs::GetInstance();
	pInputs->SampleKeyboard();

	if (theCurrentView == GALAXY)
	{
		g_theGalaxy.update(gTheMouse);
	}
	else if (theCurrentView == LIST)
	{
		g_theLists.update(gTheMouse);
	}

	gTheMouse.Update(SCREENWIDTH, SCREENHEIGHT);

	theViewButton.Update(gTheMouse);
	theOptionsButton.Update(gTheMouse);
	theExitButton.Update(gTheMouse);

	int currentView = g_theLists.GetCurrentView();

	for (int i = (currentView - 1) * 3; i < (currentView - 1) * 3+3; i++)
	{
		if (i < currentImpPlanets)
		{
			planets[i].startButton.Update(gTheMouse);
			planets[i].blurbButton.Update(gTheMouse);
		}

	}

	for (int i = 0; i < currentImpPlanets; i++)
	{
		if (planets[i].startButton.getActive() == true)
		{
			srand((unsigned)time);

			if (planets[i].theGameCreator)
				pCurrentGame = planets[i].theGameCreator();
			pCurrentGame->GameInit();
			isActive = false;
		}
	}



	if(theViewButton.getActive() == true)
	{
		if (theCurrentView == GALAXY)
		{
			theCurrentView = LIST;
		}
		else if (theCurrentView == LIST)
		{
			theCurrentView = GALAXY;
		}
	}

	thePopUps.update(gTheMouse);

	if (theOptionsButton.getActive() == true && thePopUps.theOptions.getActive() == false)
	{
		thePopUps.theOptions.setActive(true);
	}
	else if (theOptionsButton.getActive() == true && thePopUps.theOptions.getActive() == true)
	{
		thePopUps.theOptions.setActive(false);
	}

	if (theExitButton.getActive() == true && thePopUps.theExit.getActive() == false)
	{
		thePopUps.theExit.setActive(true);
	}
	else if (theExitButton.getActive() == true && thePopUps.theExit.getActive() == true)
	{
		thePopUps.theExit.setActive(false);
	}



}


void GalaxyMap::update(MousePointer gTheMouse)
{

}

void ListView::update(MousePointer gTheMouse)
{
	left.Update(gTheMouse);
	right.Update(gTheMouse);

	if (left.getActive() == true)
	{
		currentView = currentView+1;
	}
	if (right.getActive() == true)
	{
		currentView = currentView-1;
	}

}

void HubControl::draw()
{
	if (theCurrentView == GALAXY)
	{
		g_theGalaxy.draw();

		MyDrawEngine::GetInstance()->WriteText(30, 50 ,"Galaxy Map",MyDrawEngine::YELLOW,0);
	}
	else if (theCurrentView == LIST)
	{
		g_theLists.draw(planets);

		MyDrawEngine::GetInstance()->WriteText(30, 50 ,"List View",MyDrawEngine::YELLOW,0);

		MyDrawEngine::GetInstance()->WriteText(100, 165 ,"Score: ",MyDrawEngine::YELLOW,0);
		MyDrawEngine::GetInstance()->WriteInt(160, 165 , currentScore ,MyDrawEngine::YELLOW,0);
	}

	thePopUps.draw();

	theViewButton.Draw();
	theOptionsButton.Draw();
	theExitButton.Draw();



	gTheMouse.Draw();
}

void GalaxyMap::draw() 
{
	MyDrawEngine::GetInstance()->DrawAt(Vector2D(SCREENWIDTH/2, SCREENHEIGHT/2), theMainMap,screen);
}

bool truncCheck(float num, float divideNum)
{
	num = num / divideNum;

	int checkNum = (int)num;

	if (checkNum != num)
	{
		return true;
	}

	return false;
}


void ListView::draw(planetView planets[currentImpPlanets])
{
	MyDrawEngine::GetInstance()->DrawAt(Vector2D(SCREENWIDTH/2, SCREENHEIGHT/2), theBackdrop,screen);

	if (currentView < currentImpPlanets / 3 || (truncCheck((float)currentImpPlanets, 3) && currentImpPlanets > 3 && currentImpPlanets/3+1 != currentView))
	{
		left.Draw();
	}

	if(currentView > 1)
	{
		right.Draw();
	}


	for (int i = 0; i < 3; i++)
	{

		if (i + (3 * (currentView - 1)) < currentImpPlanets)
		{
			planets[i + (3 * (currentView - 1))].backdrop.draw();
			planets[i + (3 * (currentView - 1))].startButton.Draw();
			planets[i + (3 * (currentView - 1))].blurbButton.Draw();
			MyDrawEngine::GetInstance()->WriteText((int)planets[i + (3 * (currentView - 1))].backdrop.getTopLeft().XValue + 20,
				(int)planets[i + (3 * (currentView - 1))].backdrop.getTopLeft().YValue + 20, planets[i + (3 * (currentView - 1))].planetName,
				MyDrawEngine::BLUE, 0);

		}
	}

}

void HubControl::processGameResult(GameResult theResult)
{

	currentScore += theResult.score;
	currentFuel -= theResult.fuelChange;
	currentMoney += theResult.moneyChange;
}

MousePointer HubControl::getMouse()
{
	return gTheMouse;
}